namespace ScrumSprintMonitor.TestbenchApp
{
    #region #using Directives

    using System;
    using System.Diagnostics.CodeAnalysis;

    using Design;

    using Implementation;
    using Implementation.AvailableTimeCalculator;
    using Implementation.Configuration;

    using Infrastructure;
    using Infrastructure.Configuration;
    using Infrastructure.IoC;

    using Microsoft.Practices.ServiceLocation;
    using Microsoft.Practices.Unity;

    using Rhino.Mocks;

    using Testbench.Impl;

    using UI.Wpf;

    #endregion

    [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses",
        Justification = "The UnityContainerAbstraction will create this class")]
    internal class UnityAppConfigurationDependentModule : StandardModule
    {
        /// <summary>
        ///   Loads the module into the container.
        /// </summary>
        public override void Load()
        {
            SetupRuntimeBindings();
        }

        private void SetupRuntimeBindings()
        {
            var connection = Container.Resolve<IConnection>();

            Container.RegisterType<IBuildInfo, TestBuildInfo>();
            Container.RegisterType<IReportableData, ReportableData>(new ContainerControlledLifetimeManager(),
                                                                    new InjectionConstructor(new ResolvedParameter<IServiceLocator>(),
                                                                                             new InjectionParameter<IConnection>(connection),
                                                                                             new ResolvedParameter<ISprintConfiguration>()));
            Container.RegisterType<ISprint, Sprint>(new InjectionMethod("Initialize", connection));
            Container.RegisterType<ISprintBurndown, SprintBurndown>();
            Container.RegisterType<ITeamMember, TeamMember>();
            Container.RegisterType<ITeamMemberRemainingWorkload, TeamMemberRemainingWorkload>();
            Container.RegisterType<IRemainingWorkStatusEvaluator, RemainingWorkStatusEvaluator>(new ContainerControlledLifetimeManager());
            /*if (Settings.Default.EnableOutlookIntegration)
            {
                Container.RegisterType<ITeamMemberAvailableTimeCalculator, FreeBusyTeamMemberAvailableTimeCalculator>(new ContainerControlledLifetimeManager());
                Container.RegisterType<ITeamMemberFreeBusyInfo, TeamMemberOutlookFreeBusyInfo>(new ContainerControlledLifetimeManager());
                Container.RegisterType<IFreeBusyInfoConfiguration, OutlookFreeBusyInfoConfiguration>(new ContainerControlledLifetimeManager());
            }
            else*/
            {
                var freeBusyInfoConfig = MockRepository.GenerateStub<IFreeBusyInfoConfiguration>();
                freeBusyInfoConfig.Stub(m => m.ExcludeWeekends).Return(true);
                Container.RegisterInstance(freeBusyInfoConfig);

                Container.RegisterType<ITeamMemberAvailableTimeCalculator, SimpleTeamMemberAvailableTimeCalculator>(
                    new ContainerControlledLifetimeManager());
                Container.RegisterInstance<ITeamMemberFreeBusyInfo>(new TeamMemberFreeBusyInfo(freeBusyInfoConfig));
            }

            var workItemInterpretationConfiguration = new WorkItemInterpretationConfiguration
                                                          {
                                                              AllowUnestimatedWorkItems = true
                                                          };
            Container.RegisterInstance<IWorkItemInterpretationConfiguration>(workItemInterpretationConfiguration);

            var shellLayoutConfiguration = new ShellLayoutConfiguration(true, false, true, SprintMode.Sprinting);
            Container.RegisterInstance<IShellLayoutConfiguration>(shellLayoutConfiguration);

            SetupConfigurationBindings();
            SetupPresentationSettingsBindings();
            SetupTeamInformationBindings();
        }

        private void SetupPresentationSettingsBindings()
        {
            //string fullPath = Path.GetFullPath(Settings.Default.PresentationConfigurationFilePath);

            //if (!File.Exists(fullPath))
            //{
            //    throw new FileNotFoundException(
            //        string.Format("Could not find the presentation settings file at {0}.", Settings.Default.TeamMembersConfigurationFilePath), fullPath);
            //}

            Container.RegisterType<IPresentationConfiguration, TestAssetLibrary.HardcodedPresentationConfiguration>(
                new ContainerControlledLifetimeManager());
            //Bind<IPresentationConfiguration, StreamBasedPresentationConfiguration>(new ContainerControlledLifetimeManager()).WithArgument("streamProvider",
            //                                                                                                                      new ConfigurationFileStreamProvider
            //                                                                                                                        (fullPath));
        }

        private void SetupConfigurationBindings()
        {
            //string fullPath = Path.GetFullPath(Settings.Default.ConfigurationFilePath);

            //if (!File.Exists(fullPath))
            //{
            //    throw new FileNotFoundException(
            //        string.Format("Could not find the configuration file at {0}.", Settings.Default.TeamMembersConfigurationFilePath), fullPath);
            //}

			var sprintConfiguration = TestAssetLibrary.CreateSprintConfiguration();
            sprintConfiguration.SprintStartDate = new DateTime(2009, 08, 10, 8, 0, 0);
            sprintConfiguration.SprintEndDate = new DateTime(2009, 08, 24, 17, 0, 0);
            Container.RegisterInstance(sprintConfiguration);
        }

        private void SetupTeamInformationBindings()
        {
            //string fullPath = Path.GetFullPath(Settings.Default.TeamMembersConfigurationFilePath);

            //if (!File.Exists(fullPath))
            //{
            //    throw new FileNotFoundException(
            //        string.Format("Could not find the team information xml file at {0}.", Settings.Default.TeamMembersConfigurationFilePath), fullPath);
            //}

            //IDataStreamProvider xsdStreamProvider = null;
            //string xsdPath = Path.Combine(Path.GetDirectoryName(Settings.Default.TeamMembersConfigurationFilePath), @"team-info.xsd");
            //if (!File.Exists(xsdPath))
            //{
            //    xsdPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"team-info.xsd");
            //}
            //if (File.Exists(xsdPath))
            //{
            //    xsdStreamProvider = new ConfigurationFileStreamProvider(xsdPath);
            //}
            //var teamMemberInfoProviderParams = new Dictionary<string, object>
            //                                    {
            //                                        {"xmlStreamProvider", new ConfigurationFileStreamProvider(fullPath)},
            //                                        {"xsdStreamProvider", xsdStreamProvider}
            //                                    };

            //Bind<ITeamMemberInfoProvider, XmlStreamBasedTeamInfoProvider>(new ContainerControlledLifetimeManager()).WithArguments(teamMemberInfoProviderParams);

            //Bind<ITeamMemberInformation, TeamMemberInformation>();
            Container.RegisterInstance(TestAssetLibrary.CreateTeamMemberInfoProvider());
        }
    }
}